﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITAP.ContourBuilder.Components;

using ITAP.ContourBuilder.Manipulation;
using ITAP.ContourBuilder.Operations;
using ITAP.ContourBuilder.Components.Dimensions;
using ITAP.ContourBuilder.Utils;
using System.IO;

namespace ITAP.ContourBuilder.GUI
{
    public partial class ContourFormBase : Form
    {
        public ContourControl ContourControl
        {
            get
            {
                return this.contourControl;
            }
        }

        public ContourFormBase()
        {
            InitializeComponent();
            if (GlobalData.Debug)
                debugToolStripMenuItem.Visible = true;
            else
                debugToolStripMenuItem.Visible = false;
            this.contourControl.ContourContainer = ContourElementFactory.CreateContainer(0, 0, 0, 0);

            ExecutableControlManager.BuildExecutableToolstrip(toolStrip1, this.contourControl);

        }




        private void contourControl_MouseDown(object sender, MouseEventArgs e)
        {
            //ModelPoint point = this.contourControl.CoordinateConverter.ScreenPointToModelPoint(e.Location, this.contourControl.Width, this.contourControl.Height, this.contourControl.ContourContainer.BoundingBox, this.contourControl.CameraCenter);
            //IEnumerable<IContourComponent> allComponents = this.contourControl.ContourContainer.GetAllInnerComponents();
            //foreach (IContourComponent component in allComponents)
            //    component.SelectByPoint(point);
            //this.contourControl.Invalidate();
        }


        private void polylineToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
        }

        private void holeToolStripMenuItem_Click_1(object sender, EventArgs e)
        {

        }

        private void undoToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.contourControl.Manipulator.Undo();
        }

        private void redoToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.contourControl.Manipulator.Redo();
        }

        private void disableCursorMovementToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void formInputArcToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void selectByPointToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void moveToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void updateModelToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void drawTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void terminateDrawTestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.contourControl.Manipulator.Terminate(typeof(CommandDrawTest));
        }

        private void terminateSelectingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.contourControl.Manipulator.Terminate(typeof(CommandSelectComponents));
        }

        private void commandStackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CommandStack form = new CommandStack(this.contourControl.Manipulator);
            form.ShowDialog();
        }

        private void ContourFormBase_ResizeBegin(object sender, EventArgs e)
        {
            this.contourControl.DisableObtainingPeripheryState = true;
        }

        private void ContourFormBase_ResizeEnd(object sender, EventArgs e)
        {
            this.contourControl.DisableObtainingPeripheryState = false;
        }

        private void ContourFormBase_Resize(object sender, EventArgs e)
        {

        }

        private void ContourFormBase_SizeChanged(object sender, EventArgs e)
        {

        }

        private void ContourFormBase_MaximumSizeChanged(object sender, EventArgs e)
        {

        }

        private void changeTextDirectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (IContourComponent component in this.contourControl.ContourContainer.GetAllInnerComponents())
                if (component is Dimension)
                    (component as Dimension).ChangeTextDirection = !(component as Dimension).ChangeTextDirection;
            this.contourControl.UpdateModel();
        }

        private void getInvertContainerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.contourControl.ContourContainer = ContourUtils.GetInvertContainer(this.contourControl.ContourContainer);
            this.contourControl.Invalidate();
        }

        private void saveToANZToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<string> anz = this.contourControl.ContourContainer.GetAnzData();
            string allAnz = "";
            foreach (string item in anz)
                allAnz += item + "\r\n";

            File.WriteAllText("C:\\test.anz", allAnz);
        }

        private void cutViewerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //CutViewer viewer = new CutViewer();
            //viewer.ShowDialog();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void сохранитьКакDXFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "DXF files (*.dxf)|*.dxf";
            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            this.contourControl.SaveToDxf(dialog.FileName);

        }

        private void открытьDXFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "DXF files (*.dxf)|*.dxf";
            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            this.contourControl.LoadFromDxf(dialog.FileName);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void открытьANZToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void offsetContainerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.contourControl.ContourContainer = this.contourControl.ContourContainer.GetOffsetContainer();
        }

        private void сохранитьКакANZToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
